//引入请求文件
var util = require('../../utils/util.js');
var qiniuUploader = require('../../utils/qiniuUploader.js');
let app = getApp();
let imgUrl = app.imgUrl;
let url = app.url;
let gobalData = app.gobalData;
Page({
	data: {
		userAvatar: '',
		explain: "",
		images: [],
		navData: [],
		labelList: [],
		furnitureId: 0,
		currentTab: 0,
		navScrollLeft: 0,
		date: "今天",
		Price: "",
		cid: "",
		id: 0
	},
	clicks: function(e) {
		let index = e.currentTarget.dataset.index; //获取你点击的索引下标
		let arrs = this.data.labelList;
		arrs.forEach((item, i) => {
			arrs[i].status = 0;
		})
		arrs[index].status = 1;
		this.setData({
			labelList: arrs
		})
	},
	bindDateChange: function(e) {
		this.setData({
			date: e.detail.value
		})
	},
	//事件处理函数
	onLoad: function(options) {
		this.setData({
			id: options.id
		})
		util(`${url}Billing/getBillingList`, {
			id: this.data.id
		}).then(res => {
			this.setData({
				explain: res.content.consumerGoods,
				date: res.content.date,
				Price: res.content.price,
				images: res.content.picture
			})
		})
		// 获取用户头像以及昵称
		this.setData({
			userName: wx.getStorageSync('userInfo').userInfo.name,
			userAvatar: wx.getStorageSync('userInfo').userInfo.avatar,
			cid: wx.getStorageSync('userInfo').userInfo.id
		})
		// 获取接口信息---装修tab列表
		util(`${url}Furniture/getFurnitureClassificationList`).then(res => {
			if (res.status == 1) {
				this.setData({
					navData: res.content.data
				})
			} else {
				wx.showToast({
					title: "获取数据失败",
					icon: "none"
				})
			}
		})
		// 获取tab列表详情
		util(`${url}Furniture/getFurnitureList`, {
			furnitureClassificationId: 4
		}).then(res => {
			if (res.status == 1) {
				this.setData({
					labelList: res.content.data
				})
			} else {
				wx.showToast({
					title: "获取数据失败",
					icon: "none"
				})
			}
		})
	},
	// 获取单选数据
	radioLabel(e) {
		let arr = this.data.labelList;
		let furnitureId = this.data.furnitureId;
		arr.forEach(item => {
			if (item.furnitureName == e.detail.value) {
				furnitureId = item.id
			}
		})

		this.setData({
			furnitureId
		})
	},
	// 获取金额数据
	getPrice(e) {
		this.setData({
			Price: e.detail.value
		})
	},
	// 获取备注数据
	getExplain(e) {
		this.setData({
			explain: e.detail.value
		})
	},
	// 提交表单
	formSubmit: function(e) {
		// 上传图片
		let arr = [];
		let self = this;
		let obj = {
			cid: this.data.cid,
			date: this.data.date,
			price: this.data.Price,
			consumerGoods: this.data.explain,
			furnitureId: this.data.furnitureId,
			picture: this.data.images,
			id: this.data.id
		}

    util(`${url}Billing/editFurniture`, obj).then(res => {
			if (res.status == 1) {
				wx.navigateBack({
					url: '/pages/renovationBill/index'
				})
			} else if (res.msg == "缺少分类主键！"){
				wx.showToast({
					title: "修改账单请更新分类列表！",
					icon: "none"
				})
			} else {
				wx.showToast({
					title: "请确认时间，价钱，分类是否填写",
					icon: "none"
				})
			}
		})

	},
	// 再记一笔
	againSubmit() {
		let obj = {
			cid: this.data.cid,
			date: this.data.date,
			price: this.data.Price,
			consumerGoods: this.data.explain,
			furnitureId: this.data.furnitureId
		}
		util(`${url}Billing/addFurniture`, obj).then(res => {
			if (res.status == 1) {
				wx.showToast({
					title: "记录成功！"
				})
				this.setData({
					explain: "",
					date: "今天",
					Price: ""
				})
			} else {
				wx.showToast({
					title: "请确认时间，价钱，分类是否填写",
					icon: "none"
				})
			}
		})
	},
	// 滑动效果
	switchNav(event) {
		// 获取tab列表详情
		util(`${url}Furniture/getFurnitureList`, {
			furnitureClassificationId: this.data.navData[event.currentTarget.dataset.current].id
		}).then(res => {
			if (res.status == 1) {
				this.setData({
					labelList: res.content.data
				})
			} else {
				wx.showToast({
					title: "获取数据失败",
					icon: "none"
				})
			}
		})
		var cur = event.currentTarget.dataset.current;
		//每个tab选项宽度占1/4
		var singleNavWidth = this.data.windowWidth / 4;
		//tab选项居中                            
		this.setData({
			navScrollLeft: (cur - 2) * singleNavWidth
		})
		if (this.data.currentTab == cur) {
			return false;
		} else {
			this.setData({
				currentTab: cur
			})
		}
	},
	switchTab(event) {
		// 获取tab列表详情
		util(`${url}Furniture/getFurnitureList`, {
			furnitureClassificationId: this.data.navData[event.detail.current].id
		}).then(res => {
			if (res.status == 1) {
				this.setData({
					labelList: res.content.data
				})
			} else {
				wx.showToast({
					title: "获取数据失败",
					icon: "none"
				})
			}
		})
		var cur = event.detail.current;
		var singleNavWidth = this.data.windowWidth / 4;
		this.setData({
			currentTab: cur,
			navScrollLeft: (cur - 2) * singleNavWidth
		});
	},
	// 判断用户选择删除还是预览
	handleImagePreview(e) {
		wx.showModal({
			title: '删除，预览',
			content: '删除当前图片，预览当前图片',
			cancelText: '删除',
			cancelColor: '#fa863d',
			confirmText: '预览',
			confirmColor: '#fa863d',
			success: res => {
				// 预览
				if (res.confirm) {
					const idx = e.target.dataset.idx
					const images = this.data.images
					wx.previewImage({
						current: images[idx], //当前预览的图片
						urls: images, //所有要预览的图片
					})
				} else { //删除
					const idx = e.target.dataset.idx
					this.data.images.splice(idx, 1);
					this.setData({
						images: this.data.images
					})
				}
			}
		})
	}

})
